﻿using System;
using System.Data;
using System.Text;
using BusinessLogic;
using System.Web.UI;

namespace Interface
{
    public partial class Home : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        DataValid dv = new DataValid();
    
        protected void Page_Load(object sender, EventArgs e)
        {
            loadHome();
        }

        //protected void loadUC()
        //{
        //    Control ct = Page.LoadControl("Slider.ascx");
        //}

        protected void loadHome()
        {
            string sqlStr = "Select TOP 5 * from BoardingHouse where RoomAvailable > 0 Order By Viewed DESC"; ;
            DataTable dt = dc.getData(sqlStr);
           
            StringBuilder houseList = new StringBuilder();
           
            
            string _address = null;
        
            if (dt.Rows.Count > 0)
            {

                DataRow row = dt.Rows[0];
                string _Street = row["Street"].ToString();
                string _Ward = row["Ward"].ToString();
                
                int _idDistrict = Convert.ToInt32(row["District"]);
                string strsql = "Select Name from District where  IdDistrict ='" + _idDistrict + "'";
                string _dis = dc.getStringValue(strsql);


                int _idCity = Convert.ToInt32(row["City"]);
                string strsql1 = "Select Name from City where  IdCity ='" + _idCity + "'";
                string _city = dc.getStringValue(strsql1);

                _address = "Nhà trọ tại: " + _Street + ", P." + _Ward + ", " + _dis + ", TP. " + _city;
            }
                
            if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int stt = i + 1;
                        DataRow row = dt.Rows[i];

                        string _idHouse = row["IdHouse"].ToString();
                        
                        int _view = Convert.ToInt32(row["Viewed"]);
                        
                        string _contact = row["Contact"].ToString();
                        
                        int _idcat = Convert.ToInt32(row["Category"]);
                        string strsql4 = "Select Name from BoardingHouse BH, Category CT where BH.Category = CT.IdCategory and BH.IdHouse ='" +_idHouse + "'";
                        string _catname = dc.getStringValue(strsql4);

                        string imgHouse = row["ImageSource"].ToString();
                        string _houseDetail = "Houses/HouseDetail.aspx?ID=" + _idHouse;
                        string _imgHouse = "/Images/Houses/" + imgHouse;

                        houseList.AppendFormat(@"<div class='RL-box-border'>
                                <div class='RL-title'><span class='maukhac'>{0}</span> 
                                </div> 
                                <div class='small-pace'></div>                                
                                <div class='RL-image'><img width='80' height='82' src=""{1}""> </div>
                                <div class='RL-detail'>Loại Nhà Trọ: <span class='maukhac'>{2}</span></div>                                 
                                <div class='RL-detail'>Liên hệ: <span class='maukhac'>{3}</span></div>
                                <div class='RL-detail'><span class='maukhac'></span>
                                <div class='RL-detail-link'><a href=""{4}"" >Chi tiết</a></div></div></div> 
                                <div class='small-pace'></div>", _address, _imgHouse, _catname, _contact, _houseDetail);

                        lit.Text = houseList.ToString();
                }
            }
            
        }
    }
} 
